﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 客户端
{
    public partial class pay : Form
    {
        public static string gid;
        public static int price;
        public static string  num;
        string constr = login.constr;
        DataSet myst = new DataSet();
        public static string Caccount;
        public static string  Coid;
        public static string flag;
        int gammount;


        public pay()
        {
            InitializeComponent();
            发票.dj=lb_dj.Text = price.ToString();    //单价
            发票.sl=lb_sl.Text = num;      //数量
            int num2 = int.Parse(num);
            发票.zje=lb_zje.Text = (num2 * price).ToString();//总金额
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            string sql = "select gname from Goods where gid='"+gid+"'";
            SqlCommand sqlcmd = new SqlCommand(sql,sqlcon);
            SqlDataReader Sdr = sqlcmd.ExecuteReader();
            while (Sdr.Read())
            {
                发票.name=lb_mc.Text= Sdr["gname"].ToString();//商品名称
            }
            sqlcon.Close();


        }

        private void btn_pay_Click(object sender, EventArgs e)
        {
            if (combox_select.Text == "")
            {
                MessageBox.Show("请选择支付方式！");
                return;
            }
            else
            {
                SqlConnection sqlcon = new SqlConnection(constr);
                Random rd = new Random();
                Coid = rd.Next().ToString();
                string Codate = DateTime.Now.ToLocalTime().ToString(); //获取当前时间
                发票.jzfs = combox_select.Text;
                发票.Coid = Coid;
                try
                {
                    string sql = "insert into Corder(Coid,Caccount,gid,Codate,Coamount,Costate) values('" + Coid + "','" + Caccount + "','" + gid + "','" + Codate + "'," + int.Parse(lb_sl.Text.ToString()) + ",'已支付')";
                    sqlcon.Open();
                    SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
                    sqlcmd.ExecuteNonQuery();
                    sqlcon.Close();
                    this.Hide();
                    发票 发票 = new 发票();
                    发票.Show();

                string ammount = "select gammount from Goods where gid='"+gid+"'";
                sqlcon.Open();
                SqlCommand cmd = new SqlCommand(ammount,sqlcon);
                SqlDataReader Sdr = cmd.ExecuteReader();
                while (Sdr.Read())
                {
                     gammount= int.Parse(Sdr["gammount"].ToString());//商品库存数量
                }
                sqlcon.Close();
                int sy = gammount - int.Parse(lb_sl.Text.ToString());
                string update = "update Goods set gammount= "+sy+"where gid='"+gid+"'";
                sqlcon.Open();
                SqlCommand updatecmd = new SqlCommand(update,sqlcon);
                updatecmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString() + "支付失败");
                }
                finally
                {
                    sqlcon.Close();
                }
            }
        }
    }
}
